package TouristManagementBO.VisitManagement;


import org.omg.CosNaming.NamingContext;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;

import TouristManagement.ServerNamingTourist;
import TouristManagement.ServerNamingTouristHelper;

public class VisitManagementServer {
	
	private ServerNamingTourist leServerNamingTourist;	
	
	public void initServer() {
		this.connectToServerNamingTourist();
	}

	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	public void run(){
		
		try {				            
	            String args[] = null;
	        
		        // Intialisation de l'ORB
		        //************************
		        org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

		        // Gestion du POA
		        //****************
		        // Recuperation du POA
		        POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));

		        // Creation du servant
		        //*********************
		        VisitManagementImpl leVisitManagement = new VisitManagementImpl();
		        
		        // Activer le servant au sein du POA et recuperer son ID
		        rootPOA.activate_object(leVisitManagement);

		        // Activer le POA manager
		        rootPOA.the_POAManager().activate();

		
		        // Enregistrement dans le service de nommage
		        //*******************************************
		        // Recuperation du naming service
		        NamingContext nameRoot=org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

		        // Construction du nom a enregistrer
		        org.omg.CosNaming.NameComponent[] nameToRegister = new org.omg.CosNaming.NameComponent[1];
		        //enregister dans le Naming service
		        nameToRegister[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomVisitManagement(),"");

		        // Enregistrement de l'objet CORBA dans le service de noms
		        nameRoot.rebind(nameToRegister,rootPOA.servant_to_reference(leVisitManagement));

		        System.out.println("Objet "+ leServerNamingTourist.nomVisitManagement() + " created.");
		        
		        // Lancement de l'ORB et mise en attente de requete
		        //**************************************************
		        orb.run();
		    }
			catch (Exception e) {
				e.printStackTrace();
			}
		} // main
	
	public static void main(String[] args) {
		
		VisitManagementServer leVisitManagement = new VisitManagementServer();
		leVisitManagement.initServer();
		leVisitManagement.run();		
	}

}
